<template>
  <div class="user">
    <PageSearch
      :searchFormConfig="searchFormConfig"
      @resetBtnClick="handleResetClick"
      @queryBtnClick="handleQueryClick"
    ></PageSearch>
    <div class="content">
      <PageContent
        ref="pageContentRef"
        :contentTableConfig="contentTableConfig"
        :pageName="pageName"
        @newBtnClick="handleNewData"
        @editBtnClick="handleEditData"
      ></PageContent>
    </div>
    <div class="page-modal">
      <PageModal
        :modalConfig="modalConfigRef"
        ref="pageModalRef"
        :defaultInfo="defaultInfo"
        pageName="users"
      ></PageModal>
    </div>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent } from "vue";
import { searchFormConfig } from "./config/searchConfig";
import { contentTableConfig } from "./config/contentConfig";
import PageSearch from "@/components/page-search";
import PageContent from "@/components/page-content";
import { usePageSearch } from "@/hooks/usePageSearch";
import PageModal from "@/components/page-modal";
import { modalConfig } from "@/views/main/system/user/config/modalConfig";
import { usePageModal } from "@/hooks/use-page-modal";
import { useStore } from "vuex";
export default defineComponent({
  name: "User",
  components: {
    PageModal,
    PageSearch,
    PageContent,
  },
  setup() {
    const pageName = "users";
    const newCallback = () => {
      const passwordItem = modalConfig.formItems.find(
        (item) => item.field == "password"
      );
      console.log("passwordItem1", passwordItem);
      passwordItem!.isHidden = false;
      console.log("passwordItem2", passwordItem);
      console.log("执行了newCb");
    };
    const editCallback = () => {
      const passwordItem = modalConfig.formItems.find(
        (item) => item.field == "password"
      );
      passwordItem!.isHidden = true;
      console.log("passwordItem", passwordItem);
      console.log("执行了editCb");
    };
    // 2.动态添加部门和角色列表
    const store = useStore();
    const modalConfigRef = computed(() => {
      const departmentItem = modalConfig.formItems.find(
        (item) => item.field === "departmentId"
      );
      departmentItem!.options = store.state.entireDepartment.map(
        (item: any) => {
          return { title: item.name, value: item.id };
        }
      );
      const roleItem = modalConfig.formItems.find(
        (item) => item.field === "roleId"
      );
      roleItem!.options = store.state.entireRole.map((item: any) => {
        return { title: item.name, value: item.id };
      });
      return modalConfig;
    });
    //引用hooks里的函数
    const [pageContentRef, handleResetClick, handleQueryClick] =
      usePageSearch();
    const [pageModalRef, defaultInfo, handleNewData, handleEditData] =
      usePageModal(newCallback, editCallback);
    return {
      pageName,
      searchFormConfig,
      contentTableConfig,
      pageContentRef,
      handleResetClick,
      handleQueryClick,
      modalConfig,
      handleEditData,
      handleNewData,
      pageModalRef,
      defaultInfo,
      newCallback,
      editCallback,
      modalConfigRef,
    };
  },
});
</script>

<style scoped></style>
